/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.front.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.front.entity.InTheme;
import com.thinkgem.jeesite.modules.front.service.InThemeService;

/**
 * 兴趣测试题管理Controller
 * @author 朱俊吉
 * @version 2017-11-28
 */
@Controller
@RequestMapping(value = "${adminPath}/front/inTheme")
public class InThemeController extends BaseController {

	@Autowired
	private InThemeService inThemeService;
	
	@ModelAttribute
	public InTheme get(@RequestParam(required=false) String id) {
		InTheme entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = inThemeService.get(id);
		}
		if (entity == null){
			entity = new InTheme();
		}
		return entity;
	}
	
	@RequiresPermissions("front:inTheme:view")
	@RequestMapping(value = {"list", ""})
	public String list(InTheme inTheme, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<InTheme> page = inThemeService.findPage(new Page<InTheme>(request, response), inTheme); 
		model.addAttribute("page", page);
		return "modules/front/inThemeList";
	}

	@RequiresPermissions("front:inTheme:view")
	@RequestMapping(value = "form")
	public String form(InTheme inTheme, Model model) {
		model.addAttribute("inTheme", inTheme);
		return "modules/front/inThemeForm";
	}

	@RequiresPermissions("front:inTheme:edit")
	@RequestMapping(value = "save")
	public String save(InTheme inTheme, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, inTheme)){
			return form(inTheme, model);
		}
		inThemeService.save(inTheme);
		addMessage(redirectAttributes, "保存兴趣测试题成功");
		return "redirect:"+Global.getAdminPath()+"/front/inTheme/?repage";
	}
	
	@RequiresPermissions("front:inTheme:edit")
	@RequestMapping(value = "delete")
	public String delete(InTheme inTheme, RedirectAttributes redirectAttributes) {
		inThemeService.delete(inTheme);
		addMessage(redirectAttributes, "删除兴趣测试题成功");
		return "redirect:"+Global.getAdminPath()+"/front/inTheme/?repage";
	}

}